using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pig : MonoBehaviour
{
    public float maxspeed = 8;
    public float minspeed = 5;
    private SpriteRenderer spr;
    public Sprite hurt;
    public GameObject boom;
    public GameObject score;
    public bool Ispig = false;
    public AudioClip hurtCollision;
    public AudioClip dead;
    public AudioClip birdCollision;
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            AudioPlay(birdCollision);
            collision.gameObject.GetComponent<Bird>().Hurt();
        }
        if(collision.relativeVelocity.magnitude > maxspeed)
        {
            Dead();
        }else if(collision.relativeVelocity.magnitude > minspeed &&collision.relativeVelocity.magnitude < maxspeed)
        {
            if(Ispig)
            {
                spr.sprite = hurt;
                AudioPlay(hurtCollision);
            }
        }
    }

    private void Awake()
    {
        spr = GetComponent<SpriteRenderer>();
    }

    public void Dead()
    {
        if(Ispig)
        {
            GameManager.instance.pigs.Remove(this);
        }
        Destroy(gameObject);
        Instantiate(boom,transform.position,Quaternion.identity);
        Instantiate(score,transform.position+new Vector3(0,0.5f,0),Quaternion.identity); 
        AudioPlay(dead);
    }
    public void AudioPlay(AudioClip audioClip)
    {
        AudioSource.PlayClipAtPoint(audioClip, transform.position);
    }
}
